package com.jalarbee.web.jsf.controller;

import com.jalarbee.stock.sales.business.Sale;
import com.jalarbee.stock.sales.service.SaleService;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.faces.bean.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author Abdoulaye <b>Ikhnaton</b> Diallo
 */

@Named
@RequestScoped
public class SalesController {

    @Inject
    private SaleService saleService;
    
    private List<Sale> sales = new ArrayList<Sale>();
    private Set<Long> keys = new LinkedHashSet<Long> ();

    public SalesController() {
    }

    public List<Sale> getSales() {
        if(sales.isEmpty()) {
            sales = saleService.getSales();
        }
        initKeys(sales);
        return sales;
    }
    
    private void initKeys(List<Sale> sales) {
        keys.clear();
        for(Sale s : sales) {
            keys.add(s.getId());
        }
    }

    public Set<Long> getKeys() {
        return keys;
    }
    
}
